package com.ctgu.computer.controller.topic;

import cn.hutool.http.server.HttpServerRequest;
import com.ctgu.computer.common.FailedMessage;
import com.ctgu.computer.common.SuccessMessage;
import com.ctgu.computer.common.UserLocalTread;
import com.ctgu.computer.domain.dto.TopicAddDTO;
import com.ctgu.computer.domain.dto.TopicPageDTO;
import com.ctgu.computer.domain.po.ChannelInfo;
import com.ctgu.computer.domain.po.TextInfo;
import com.ctgu.computer.domain.result.PageResult;
import com.ctgu.computer.domain.result.Result;
import com.ctgu.computer.domain.vo.ImagePaths;
import com.ctgu.computer.domain.vo.TopicVO;
import com.ctgu.computer.service.TopicService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sys/topic")
@RequiredArgsConstructor
@Tag(name = "帖子相关接口")
public class TopicController {

    private final TopicService topicService;

    @Operation(summary = "分页查询帖子")
    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody TopicPageDTO topicPageDTO){
        PageResult pageResult = topicService.page(topicPageDTO);
        return Result.success(pageResult);
    }

    @Operation(summary = "新增帖子")
    @PostMapping("/add")
    public Result addTopic(@RequestBody TopicAddDTO topicAddDTO){
        topicService.addTopic(topicAddDTO);
        return Result.success(SuccessMessage.ADD_SUCCESS,null);
    }

    @Operation(summary = "新增频道")
    @GetMapping("/channel/add/{channelName}")
    public Result addChannel(@PathVariable String channelName){
        // 只有管理员有权限新增频道
        if(UserLocalTread.getAdminId()==null){
            return Result.error(FailedMessage.NO_PERMISSION);
        }
        topicService.addChannel(channelName);
        return Result.success(SuccessMessage.ADD_SUCCESS,null);
    }

    @Operation(summary = "删除帖子")
    @GetMapping("/delete")
    public Result deleteText(@RequestParam Integer textId){
        topicService.deleteText(textId);
        return Result.success(SuccessMessage.DELETE_SUCCESS,null);
    }

    @Operation(summary = "查询所有频道")
    @GetMapping("/channel/all")
    public Result<List<ChannelInfo>> getAllChannel(){
        return Result.success( topicService.getAllChannel());
    }

    @Operation(summary = "根据id查询帖子")
    @GetMapping("/select/{textId}")
    public Result<TopicVO> getById(@PathVariable Integer textId){
        return Result.success(topicService.getById(textId));
    }

    @Operation(summary = "点赞文章")
    @PostMapping("/good/{textId}")
    public Result goodById(@PathVariable Integer textId){
        topicService.good(textId);
        return Result.success();
    }

    @Operation(summary = "图片上传")
    @PostMapping("/upload")
    public Result<ImagePaths> imageUpload(@RequestParam("files") MultipartFile[] files){
        for (MultipartFile file : files) {
            if(!file.isEmpty()){
                if(!file.getOriginalFilename().contains(".png")&&!file.getOriginalFilename().contains(".jpg")){
                    return Result.error(FailedMessage.ONLY_IMAGE_TYPE);
                }
            }
        }
       return Result.success( topicService.addImages(files));
    }
}